package com.example.Controller;


import com.example.Service.impl.DailyServiceImpl;
import com.example.pojo.Daily;
import com.example.pojo.DailyData;
import com.example.util.Result;
import com.github.pagehelper.PageInfo;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@RestController
@RequestMapping("/daily")
public class DailyController {

    @Autowired
    private DailyServiceImpl dailyService;

    @GetMapping("/selectDaily")
    public Result selectDaily(Daily daily,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "8")Integer pageSize) {
        PageInfo<Daily> pageInfo = dailyService.selectDaily(daily,pageNum,pageSize);
        return new Result("200", "查询成功", pageInfo);
    }


    //日报的具体内容
    @GetMapping("/selectDailyViewId/{id}")
    public Result selectDailyViewId(@PathVariable Integer id) {
        Daily daily = dailyService.selectDailyViewId(id);
        return new Result("200", "查询成功", daily);
    }



    //下载功能 下载为文本 txt格式 下载为doc格式
    @GetMapping("/download-txt/{id}")
    public void downloadTxt(@PathVariable("id") Integer id, HttpServletResponse response) {
        Daily daily = dailyService.selectDailyViewId(id);
        if (daily == null) {
            throw new IllegalArgumentException("Daily with id " + id + " not found");
        }
        String content = daily.getContent(); // 假设内容是字符串

        // 设置响应头
        response.setContentType("text/plain;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=output.txt");

        // 将内容写入响应流
        try (OutputStream outputStream = response.getOutputStream()) {
            byte[] bytes = content.getBytes("UTF-8");
            outputStream.write(bytes);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            // 如果发生异常，可以在这里处理 -------------
        }
    }


    // 保存为.docx文件
    @PostMapping("/save-doc")
    public Result saveAsDoc(@RequestBody String content) {
        try (XWPFDocument document = new XWPFDocument();
            FileOutputStream out = new FileOutputStream("output.docx")) {
            document.createParagraph().createRun().setText(content);
            document.write(out);
            return new Result("200", "文件保存成功");
        } catch (IOException e) {
            return new Result("500", "文件保存失败");
        }
    }

    // 获取用户发送的日报
    @GetMapping("/myDaily/{id}")
    public Result myDaily(@PathVariable("id") Integer id) {
        PageInfo<Daily> pageInfo = dailyService.selectMyDaily(id);
        return new Result("200", "查询成功", pageInfo);
    }

    //获取用户团队发送的日报 属于用户团队的日报
    @GetMapping("/teamDaily/{employeeId}/{departmentId}")
    public Result teamDaily(@PathVariable String employeeId,
                            @PathVariable String departmentId) {
        List<Daily> dailyList = dailyService.selectTeamDaily(employeeId,departmentId);
        return new Result("200", "查询成功", dailyList);
    }



    //访问次数
    @GetMapping("/selectDailyData")
    public Result selectDailyData(DailyData dailyData,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "8")Integer pageSize) {
        PageInfo<DailyData> pageInfo = dailyService.selectDailyData(dailyData,pageNum,pageSize);
        return new Result("200", "查询成功", pageInfo);
    }

    //新增的日报
    @PostMapping("/addDaily")
    public Result addDaily(@RequestBody Daily daily) {
        int row = dailyService.insert(daily);
        if (row > 0) {
            return new Result("200", "添加成功");
        } else {
            return new Result("500", "添加失败");
        }
    }

}
